<template>
  <div>
    <h1>物品列表</h1>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="_id" label="id"> </el-table-column>
      <el-table-column prop="name" label="物品名称"> </el-table-column>
       <el-table-column label="物品图标"> 
         <template slot-scope="scope">
           <div>
             <img :src="scope.row.icon" alt="" style="height:3em">
           </div>
         </template>
       </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="echo(scope.row._id)"
            >编辑</el-button
          >
          <el-button @click="remov(scope.row._id)" type="text" size="small"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 编辑对话框 -->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <el-form ref="form" :model="editform" label-width="80px">
        <el-form-item label="修改分类">
          <el-input v-model="editform.name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="edit()"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
 <script>
export default {
  data() {
    return {
      tableData: [],
      dialogVisible: false,
      editform:{
        name:'',
        _id:''
      }
    };
  },
  created() {
    this.getlist();
  },
  methods: {
    async getlist() {
      const res = await this.$http.get("/rest/items");
      this.tableData = res.data;
      // console.log(res);
    },
    // 删除
   async remov(id) {
       // 弹框提示用户，是否要删除数据
      const confirmResult = await this.$confirm(
        "此操作将永久删除该用户, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(err => err);

      // 用户取消了删除
      if (confirmResult !== "confirm") {
        return this.$message({
          type: "info",
          message: "已取消删除"
        });
      }
      // console.log(id);
      const res =  await this.$http.delete(`/rest/items/${id}`)
      console.log(res)
      this.$message.success="成功删除"
      this.getlist()
      // this.dialogVisible = true;
    },
    // 回显
    async echo(id){
      this.dialogVisible = true;
      const res = await this.$http.get(`/rest/items/${id}`)
      // console.log(res) 
      this.editform=res.data
    },

    // 修改
   async edit() {
      const res = await this.$http.put(`/rest/items/${this.editform._id}`,this.editform)
      this.dialogVisible=false
      console.log(res)
      this.getlist()
    },

  },
};
</script>